package com.kai.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kai.demo.entity.Role;
import com.kai.demo.entity.RoleUser;
import com.kai.demo.entity.User;
import com.kai.demo.mapper.RoleMapper;
import com.kai.demo.mapper.RoleUserMapper;
import com.kai.demo.mapper.UserMapper;
import com.kai.demo.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Project: sprigncloud-demo </p>
 * <p>Description: </p>
 * <p>Copyright (c) 2024 Karrytech (Shanghai/Beijing) Co., Ltd.</p>
 * <p>All Rights Reserved.</p>
 *
 * @author <a href="mailto:zhangkai@karrytech.com">Zhang Kai</a>
 */
@Service
public class UserServiceImpl implements UserService {

    private final Logger logger = Logger.getLogger(this.getClass());

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleUserMapper roleUserMapper;
    @Resource
    private RoleMapper roleMapper;

    public void save(User user) {
        userMapper.insert(user);
    }

    public User get(Integer id) {
        try {
            return userMapper.selectById(id);
        } catch (Exception e){
            logger.info("数据查找失败！");
            return new User();
        }
    }

    @Override
    public List<User> findAll() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        return userMapper.selectList(wrapper);
    }

    @Override
    public User getByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return userMapper.selectOne(wrapper);
    }
}